package cn.hubujyjs.trainingapp.controller;

import cn.hubujyjs.trainingapp.dto.LoginDto;
import cn.hubujyjs.trainingapp.service.UserService;
import cn.hubujyjs.trainingapp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param phone 手机号
     * @return R
     */
    @PostMapping("/code")
    public R sendCode(@RequestParam("phone") String phone) {
        return userService.sendCode(phone);
    }

    /**
     * 登录
     * @param login 登录参数。手机号、验证码（或者手机号、密码）
     * @return token
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginDto login){
        return userService.login(login);
    }
    @GetMapping("/logout")
    public R logout(HttpServletRequest request){
        String authorization = request.getHeader("authorization");
        return userService.logout(authorization);
    }
}
